import string
import numpy as np
from copy import deepcopy
from typing import List


# from logger import log


class User_Node:
    def __init__(self, id: string, initial_location: np.array, sampling_interval):
        self.upload_time = 0
        self.connected_uav = None
        self.offload_partition = []
        self.final_offload_partition = 0
        self.bandwidth = 1e6  # 传输带宽 单位MHz
        self.sampling_interval = deepcopy(sampling_interval)
        self.id = deepcopy(id)
        self.initial_location = deepcopy(initial_location)
        self.location = deepcopy(self.initial_location)
        self.data_size = 2e6  # 数据大小 单位bit
        self.comp_density = 1000  # 计算密度 单位 CPU cycles/bit
        self.t_power = 0.1  # 传输功率 单位 W
        self.snr: float = 0
        self.cache_state = 0  # 根据UE本身的属性更新
        self.upload_state = 0  # 根据输入的动作进行更新
        self.AoI_UE = 0  # 根据UE的采样频率进行更新
        self.AoI_DC = 0
        self.last_update = 0
        self.other_users: List[User_Node] = []
        self.upload_ec = 0
        self.f = 2e9
        self.comp_ec = 0
        self.update_time_slot = 0  # 记录数据更新的时隙编号
        self.AoI_DC_delta = 0

    def set_other_users(self, other_users: List['User_Node']):
        self.other_users = other_users

    def reset(self):
        self.location = deepcopy(self.initial_location)
        self.cache_state = 0
        self.upload_state = 0
        self.AoI_DC = 0
        self.AoI_UE = 0
        self.update_time_slot = 0

    def cache_update(self, time_slot, seed=None):
        # 如果提供了种子，设置随机种子
        if seed is not None:
            np.random.seed(seed)

        # 更新 AoI_UE：只受采样间隔影响
        if self.last_update >= self.sampling_interval:
            # 达到采样间隔，重置 AoI_UE 为 0
            self.AoI_UE = 0
            self.last_update = 0  # 重置时间计数器
            self.update_time_slot = time_slot

            self.cache_state = 1  # 缓存状态设置为 1

        else:
            # 未达到采样间隔，AoI_UE 递增
            self.last_update += 1
            self.AoI_UE += 1  # 随时间递增 AoI_UE

        # 如果上传数据后，缓存状态变为 0
        if self.upload_state == 1:
            self.cache_state = 0  # 上传后缓存将清空为0
